﻿using System;
using System.Collections.Generic;
using System.Linq;
using FrontEnd.Models;
using FrontEnd.Utilties.Text;

namespace FrontEnd.Application.ContentService.Fake
{
    internal class FakeContentService : IContentService
    {

        #region Fake Data
        
        private static readonly IList<ProposalInfo> FakeProposals =
            new List<ProposalInfo>
            {              
                 new ProposalInfo
                 {                     
                     Name = "TestApple1",
                     Title = "Test Apple #1",
                     Content = "Test Apple Content #1",
                     UserAdds = 150
                 },
                 new ProposalInfo
                 {                       
                     Name = "TestApple2",
                     Title = "Test Apple #2",
                     Content = "Test Apple Content #2",
                     UserAdds = 2
                 }, 							 
                 new ProposalInfo
                 {               
                     Name = "TestMicrosoft1",
                     Title = "Test Microsoft #1",
                     Content = "Test Microsoft Content #1",
                     UserAdds = 145
                 },
                 new ProposalInfo
                 {
                     Name = "TestMicrosoft2",
                     Title = "Test Microsoft #2",
                     Content = "Test Microsoft Content #2",
                     UserAdds = 48
                 }
            };

		private static readonly IList<RequestInfo> FakeRequests =
			new List<RequestInfo>
            {              
                 new RequestInfo
                 {                     
                     Name = "jQuery_rulez",
                     Title = "jQuery - рулез",
                     Content = "Как было бы круто, если бы кто-то поведал о возможностях анимации в этой библиотеке!",
                     Author = new PersonInfo(),
                     CreatedDate = DateTime.Now,
                     RequestsCount = 5,
                     Comments = new List<RequestCommentInfo>(),
                     Proposals = new List<ProposalInfo>()
                 },
                 new RequestInfo
                 {                       
                     Name = "Shhshhshhshhshh_Python",
                     Title = "Shhshhshhshhshh - Python",
                     Content = "По мелодичности уступает только французскому. Поведайте о языке, а?",
                     Author = new PersonInfo {DisplayName = "Андрей Каща"},
                     CreatedDate = DateTime.Now,
                     RequestsCount = 0,
                     Comments = new List<RequestCommentInfo>(),
                     Proposals = new List<ProposalInfo>()
                 }, 							 
                 new RequestInfo
                 {               
                     Name = "Kachestvo_coda",
                     Title = "Качество кода",
                     Content = "Что является основными составляющими высококачественного кода?",
                     Author = new PersonInfo(),
                     CreatedDate = DateTime.Now,
                     RequestsCount = 2,
                     Comments = new List<RequestCommentInfo>(),
                     Proposals = new List<ProposalInfo>()
                 },
                 new RequestInfo
                 {
                     Name = "Instrumentariy_razrabotchika",
                     Title = "Инструментарий разработчика",
                     Content = "Кто-нить расскажет толком об этом?",
                     Author = new PersonInfo(),
                     CreatedDate = DateTime.Now,
                     RequestsCount = 3,
                     Comments = new List<RequestCommentInfo>(),
                     Proposals = new List<ProposalInfo>()
                 }
				 ,
                 new RequestInfo
                 {
                     Name = "Istoriya_weba",
                     Title = "История веба",
                     Content = "Как создавался? Кто афтар? Фтопку!",
                     Author = new PersonInfo(),
                     CreatedDate = DateTime.Now,
                     RequestsCount = 5,
                     Comments = new List<RequestCommentInfo>(),
                     Proposals = new List<ProposalInfo>()
                 }
            };

        #endregion
        
        public IEnumerable<ProposalInfo> EnumerateAvailableProposals()
        {
        	return FakeProposals;
        }

    	public IEnumerable<RequestInfo> EnumerateAvailableRequests()
    	{
    		return FakeRequests;
    	}

    	public int IncrementProposalUserAdds(string proposalName)
        {
			ProposalInfo proposalInfo = FakeProposals
					.FirstOrDefault(prop => prop.Name == proposalName);

			if (proposalInfo != null)
			{
				proposalInfo.UserAdds++;
				return proposalInfo.UserAdds;
			}

            return new Random().Next(255);
        }

    	public int IncrementRequestsCount(string requestName)
    	{
			RequestInfo requestInfo = FindRequest(requestName);

			if (requestInfo == null)
				return -1;
			
			requestInfo.RequestsCount++;
			return requestInfo.RequestsCount;
    	}

		public void AddRequestComment(string requestName, RequestCommentInfo requestComment)
    	{
			RequestInfo requestInfo = FindRequest(requestName);

			if (requestInfo == null)
				return;

			requestInfo.Comments.Add(requestComment);
    	}

		public void AddRequest(string requestTitle, string requestDescription)
		{
			RequestInfo newRequestInfo = new RequestInfo
				{
					Name = TextUtility.Transliterate(requestTitle),
					Title = requestTitle,
					Content = requestDescription,
					Author = new PersonInfo(), // TODO: change to identity user
					CreatedDate = DateTime.Now,
					RequestsCount = 1, // this one is also counted
					Comments = new List<RequestCommentInfo>(),
					Proposals = new List<ProposalInfo>()
				};
			FakeRequests.Add(newRequestInfo);
		}

		private RequestInfo FindRequest(string requestName)
		{
			return FakeRequests.FirstOrDefault(prop => prop.Name == requestName);
		}
    }
}